/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package xmarti.vacaxm.client.maintenance.animal;



import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import xmarti.vacaxm.action.ImplMaintenance;

import xmarti.vacaxm.beans.maintenance.AnimalCode;
import xmarti.vacaxm.beans.maintenance.Area;
import xmarti.vacaxm.beans.maintenance.Country;
import xmarti.vacaxm.client.maintenance.auxiliar.JDArea;
import xmarti.vacaxm.client.maintenance.auxiliar.JDCountry;
import xmarti.vacaxm.exception.ExceptionAnimalCode;
import xmarti.vacaxm.exception.ExceptionHibernateVacaxm;
import xmarti.vacaxm.utils.TDSLanguageUtils;
import xmarti.vacaxm.utils.ValidarText;

/**
 *
 * @author xmarti
 */
public class JDAnimalCode extends javax.swing.JDialog {

    /**
     * Creates new form JDAnimalCode
     */
    private AnimalCode animalCode;
    private ImplMaintenance aMaintenance; 
    private List<Area>arees;
    private List<Country>countries; 
    
    public JDAnimalCode(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        animalCode= new AnimalCode();
        initObjects();
        setData(); 
    }
    public JDAnimalCode(java.awt.Frame parent, boolean modal, AnimalCode animalCode) {
        super(parent, modal);
        initComponents();
        this.animalCode= animalCode;
        initObjects();
        setData(); 
    }
    private void initObjects(){
        this.aMaintenance= new ImplMaintenance(); 
        jTextCC.setDocument(new ValidarText(2,true));
        jTextAnimalCode1.setDocument(new ValidarText(4,true));
        jTextAnimalCode2.setDocument(new ValidarText(4,true));
        try {
            setCountries();
            setArees();
        } catch (ExceptionHibernateVacaxm ex) {
            Logger.getLogger(JDAnimalCode.class.getName()).log(Level.SEVERE, null, ex);
        }
        
    }  
    private void setCountries() throws ExceptionHibernateVacaxm{
        String countryText=null;
        this.countries= aMaintenance.getCountries();
        if (this.jComboCountry.getSelectedIndex()>-1){
            countryText = (String) this.jComboCountry.getSelectedItem();            
        }        
       this.jComboCountry.removeAllItems();               
       for(Country c:countries)this.jComboCountry.addItem(c.toText());        
        if (countryText!=null ){        
            for(int i=0;i<this.jComboCountry.getItemCount();i++){
                String c = (String)this.jComboCountry.getItemAt(i);
                if (c.equals(countryText)){
                    this.jComboCountry.setSelectedIndex(i);
                    i=this.jComboCountry.getItemCount();
                }                        
            }
        }
       
    }
    private void setArees() throws ExceptionHibernateVacaxm{
        String areaText=null;
        arees=aMaintenance.getAreas();        
        if (this.jComboArea.getSelectedIndex()>-1){
            areaText = (String) this.jComboArea.getSelectedItem();            
        }               
        this.jComboArea.removeAllItems();        
        for(Area c:arees)this.jComboArea.addItem(c.toText());
        
        if (areaText!=null ){
            for(int i=0;i<this.jComboArea.getItemCount();i++){                
                if (this.jComboArea.getItemAt(i).equals(areaText)){
                    this.jComboArea.setSelectedIndex(i);
                    i=this.jComboArea.getItemCount();
                }                        
            }
        }        
    }
    private void setData(){        
        jComboCountry.setSelectedItem(this.animalCode.getCountry().toText());
        jTextCC.setText(this.animalCode.getControlCode());
        jComboArea.setSelectedItem(this.animalCode.getAreaCode().toText());
        jTextAnimalCode1.setText(this.animalCode.getAnimalCode1());
        jTextAnimalCode2.setText(this.animalCode.getAnimalCode2());
  
    }
    
    private AnimalCode getData(){
        AnimalCode animalCode= new AnimalCode();        
        animalCode.setCountry(this.getCountry());
        animalCode.setControlCode(jTextCC.getText());
        animalCode.setAreaCode(this.getArea());
        animalCode.setAnimalCode1(jTextAnimalCode1.getText());        
        animalCode.setAnimalCode2(jTextAnimalCode2.getText());        
        return animalCode;
    }
    private Country getCountry(){
        Country country=null; 
        for (Country c:this. countries){
            if (c.toText().equals(jComboCountry.getSelectedItem())){
                return c; 
            }                
        }
        return country;
    }
    private Area getArea(){
        Area area=null; 
        for (Area c:this.arees){
            if (c.toText().equals(jComboArea.getSelectedItem())){
                return c; 
            }                
        }
        return area;
    }
    
    public AnimalCode getAnimalCode(){             
        return this.animalCode;
    }
    public void setAnimalCode (AnimalCode animalCode){
        this.animalCode=animalCode;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabelArea = new javax.swing.JLabel();
        jLabelCountry = new javax.swing.JLabel();
        jComboCountry = new javax.swing.JComboBox();
        jLabelAnimalCode = new javax.swing.JLabel();
        jTextCC = new javax.swing.JTextField();
        jComboArea = new javax.swing.JComboBox();
        jTextAnimalCode2 = new javax.swing.JTextField();
        jTextAnimalCode1 = new javax.swing.JTextField();
        jLabelCC = new javax.swing.JLabel();
        jButtonSave = new javax.swing.JButton();
        jButtonCancel = new javax.swing.JButton();
        jButtonArea = new javax.swing.JButton();
        jButtonCountry = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle(TDSLanguageUtils.getMessage("jDialogAnimalCode.caption"));

        jLabelArea.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabelArea.setText(TDSLanguageUtils.getMessage("jDialogAnimalCode.jLabelArea.caption"));

        jLabelCountry.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabelCountry.setText(TDSLanguageUtils.getMessage("jDialogAnimalCode.jLabelCountry.caption"));

        jComboCountry.setFont(new java.awt.Font("Book Antiqua", 1, 12)); // NOI18N
        jComboCountry.setToolTipText(TDSLanguageUtils.getMessage("jDialogAnimalCode.jComboCountry.toolTipText"));
        jComboCountry.setAlignmentX(0.0F);
        jComboCountry.setAlignmentY(0.0F);
        jComboCountry.setBorder(null);
        jComboCountry.setOpaque(false);

        jLabelAnimalCode.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabelAnimalCode.setText(TDSLanguageUtils.getMessage("jDialogAnimalCode.jLabelAnimalCode.caption"));

        jTextCC.setFont(new java.awt.Font("Book Antiqua", 1, 14)); // NOI18N
        jTextCC.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        jTextCC.setToolTipText(TDSLanguageUtils.getMessage("jDialogAnimalCode.jTextCC.toolTipText"));
        jTextCC.setAlignmentX(0.0F);
        jTextCC.setAlignmentY(0.0F);

        jComboArea.setFont(new java.awt.Font("Book Antiqua", 1, 12)); // NOI18N
        jComboArea.setToolTipText(TDSLanguageUtils.getMessage("jDialogAnimalCode.jComboArea.toolTipText"));
        jComboArea.setAlignmentX(0.0F);
        jComboArea.setAlignmentY(0.0F);
        jComboArea.setOpaque(false);

        jTextAnimalCode2.setFont(new java.awt.Font("Book Antiqua", 1, 14)); // NOI18N
        jTextAnimalCode2.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        jTextAnimalCode2.setToolTipText(TDSLanguageUtils.getMessage("jDialogAnimalCode.jTextAnimalCode2.toolTipText"));
        jTextAnimalCode2.setAlignmentX(0.0F);
        jTextAnimalCode2.setAlignmentY(0.0F);

        jTextAnimalCode1.setFont(new java.awt.Font("Book Antiqua", 1, 14)); // NOI18N
        jTextAnimalCode1.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        jTextAnimalCode1.setToolTipText(TDSLanguageUtils.getMessage("jDialogAnimalCode.jTextAnimalCode1.toolTipText"));
        jTextAnimalCode1.setAlignmentX(0.0F);
        jTextAnimalCode1.setAlignmentY(0.0F);

        jLabelCC.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabelCC.setText(TDSLanguageUtils.getMessage("jDialogAnimalCode.jLabelCC.caption"));

        jButtonSave.setText(TDSLanguageUtils.getMessage("jDialogAnimalCode.jButtonSave.caption"));
        jButtonSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonSaveActionPerformed(evt);
            }
        });

        jButtonCancel.setText(TDSLanguageUtils.getMessage("jDialogAnimalCode.jButtonCancel.caption"));
        jButtonCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonCancelActionPerformed(evt);
            }
        });

        jButtonArea.setIcon(new javax.swing.ImageIcon(getClass().getResource("/xmarti/vacaxm/image/BotoNouPetit.png"))); // NOI18N
        jButtonArea.setToolTipText(TDSLanguageUtils.getMessage("jDialogAnimalCode.jButtonArea.toolTipText"));
        jButtonArea.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonAreaActionPerformed(evt);
            }
        });

        jButtonCountry.setIcon(new javax.swing.ImageIcon(getClass().getResource("/xmarti/vacaxm/image/BotoNouPetit.png"))); // NOI18N
        jButtonCountry.setToolTipText(TDSLanguageUtils.getMessage("jDialogAnimalCode.jButtonCountry.toolTipText"));
        jButtonCountry.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonCountryActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(1, 1, 1)
                .addComponent(jLabelCountry, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jComboCountry, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jButtonCountry, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabelArea, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jComboArea, javax.swing.GroupLayout.PREFERRED_SIZE, 153, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jButtonArea, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabelCC, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jTextCC, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabelAnimalCode, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jTextAnimalCode1, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(3, 3, 3)
                        .addComponent(jTextAnimalCode2, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(35, 35, 35)
                        .addComponent(jButtonSave)
                        .addGap(76, 76, 76)
                        .addComponent(jButtonCancel)))
                .addGap(0, 0, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(11, 11, 11)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabelCountry)
                        .addComponent(jComboCountry, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jButtonCountry))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelCC)
                    .addComponent(jTextCC, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(5, 5, 5)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jComboArea, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jButtonArea)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(11, 11, 11)
                        .addComponent(jLabelArea)))
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(7, 7, 7)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jTextAnimalCode1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jTextAnimalCode2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(12, 12, 12)
                        .addComponent(jLabelAnimalCode)))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButtonSave)
                    .addComponent(jButtonCancel))
                .addGap(20, 20, 20))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonCancelActionPerformed
        this.dispose();
    }//GEN-LAST:event_jButtonCancelActionPerformed
    private void validateData(AnimalCode animalCode) throws ExceptionAnimalCode, ExceptionHibernateVacaxm {        
        if (animalCode.getCountry()==null)throw new ExceptionAnimalCode(ExceptionAnimalCode.ERR_COUNTRY);
        else if (animalCode.getAreaCode()==null)throw new ExceptionAnimalCode(ExceptionAnimalCode.ERR_AREA);
        //else if(animalCode.getCountry().getId()<=0) throw new ExceptionAnimalCode(ExceptionAnimalCode.ERR_COUNTRY);
        //else if(animalCode.getAreaCode().getId()<=0) throw new ExceptionAnimalCode(ExceptionAnimalCode.ERR_AREA);
        else if((animalCode.getControlCode().length()<2)) throw new ExceptionAnimalCode(ExceptionAnimalCode.ERR_CODE_CONTROL);
        else if((animalCode.getAnimalCode1().length()<4)) throw new ExceptionAnimalCode(ExceptionAnimalCode.ERR_CODE_ANIMAL1);
        else if((animalCode.getAnimalCode2().length()<4)) throw new ExceptionAnimalCode(ExceptionAnimalCode.ERR_CODE_ANIMAL2);
        else if(aMaintenance.existAnimalCode(animalCode)) throw new ExceptionAnimalCode(ExceptionAnimalCode.ERR_EXIST_CODE_ANIMAL);
       
    }
    private void jButtonSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonSaveActionPerformed
        // Cal guardar primer l'animalCode
        AnimalCode aC = getData();
        if(!aC.equals(this.animalCode)){
            try {
                validateData(aC);
                this.setAnimalCode(aC);
                this.dispose();
            } catch (ExceptionAnimalCode | ExceptionHibernateVacaxm ex ) {
                JOptionPane.showMessageDialog(null,ex.getMessage(),TDSLanguageUtils.getMessage("validate.data.ERR"),JOptionPane.ERROR_MESSAGE);
            }
        }else this.dispose(); 
        
    }//GEN-LAST:event_jButtonSaveActionPerformed

    private void jButtonCountryActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonCountryActionPerformed
        final JDCountry dialog = new JDCountry();               
        dialog.setLocationRelativeTo(null);
        dialog.setVisible(true);
        try {
            setCountries();
        } catch (ExceptionHibernateVacaxm ex) {
            Logger.getLogger(JDAnimalCode.class.getName()).log(Level.SEVERE, null, ex);
        }
        
    }//GEN-LAST:event_jButtonCountryActionPerformed

    private void jButtonAreaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonAreaActionPerformed
        final JDArea dialog = new JDArea();               
        dialog.setLocationRelativeTo(null);
        dialog.setVisible(true);
        try {
            setArees();
        } catch (ExceptionHibernateVacaxm ex) {
            Logger.getLogger(JDAnimalCode.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_jButtonAreaActionPerformed
//    private void jComboChange(java.awt.event.ActionEvent evt) {                                             
//        JComboBox jCombo = (JComboBox) evt.getSource();
//        if (jCombo.getSelectedIndex()==0) jCombo.setSelectedIndex(-1);
//    }   
    

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonArea;
    private javax.swing.JButton jButtonCancel;
    private javax.swing.JButton jButtonCountry;
    private javax.swing.JButton jButtonSave;
    private javax.swing.JComboBox jComboArea;
    private javax.swing.JComboBox jComboCountry;
    private javax.swing.JLabel jLabelAnimalCode;
    private javax.swing.JLabel jLabelArea;
    private javax.swing.JLabel jLabelCC;
    private javax.swing.JLabel jLabelCountry;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTextField jTextAnimalCode1;
    private javax.swing.JTextField jTextAnimalCode2;
    private javax.swing.JTextField jTextCC;
    // End of variables declaration//GEN-END:variables
}
